<?php

namespace app\xwam\controller;

/**
 * ProductCategoryController 控制器
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductCategoryController extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "title like '%$word%' or code like '%$word%'";
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $asearch = $this->indexSearch();
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $list = $mCategory->selectTree(array('where'=>$asearch['where']));
        $this->assign('search', $asearch['search']);
        $this->assign('tree', $list);
        return view();
    }

    public function edit($id = 0, $pid = 0) {
        $data = array();
        if ($id) {
            $mCategory = new \app\xwam\model\ProductCategoryModel();
            $data = $mCategory->find($id);
        }
        if (!$data) {
            $data = array('pid' => $pid, 'site_id' => $this->siteId);
        }
        $this->assign('data', $data);
        return view();
    }

    public function editSave() {
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $result = $mCategory->saveCategory();
        $this->dialogJump($result['status'], $result['info']);
    }
    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function export() {
        $asearch = $this->get_category_search_data();
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $list = $mCategory->scope('export')->where($asearch['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $mCategory->copyCategory($id);
        $this->ajaxMsg(true);
    }

    public function getTreeJson($pid = 0) {
        $options = array();
        if ($pid) {
            $options['where']['pid'] = $pid;
        }
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $data = $mCategory->selectTreeJson($options);
        $this->ajaxReturn($data);
    }

    function changeStatus($id, $status) {
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $this->ajaxReturn($mCategory->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $this->ajaxReturn($mCategory->changeSort($id, $sort));
    }

    /**
     * category_delete操作
     * @param string $id 主键编号
     * @since 1.0<2015-7-31> SoChishun Added.
     */
    public function delete($id = '') {
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $this->ajaxReturn($mCategory->deleteCtegory($id));
    }

    // 2016-8-23 SoChishun Added.
    function brand_picker($id, $brand_id = 0) {
        $this->assign('brand_id', $brand_id);
        $this->assign('cid', $id);
        return view();
    }

    public function brand_picker_save() {
        $ids = I('id');
        $cid = I('cid');
        if (!$ids || !$cid) {
            $this->error('参数有误!');
        }
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $result = $mCategory->where(array('id' => $cid))->setField('brand_id', $ids);
        if (false === $result) {
            $this->error('出错啦：' . $mCategory->getDbError());
        } else {
            $this->dialogJump(true);
        }
    }

    function specification_picker($id) {
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $specification_ids = $mCategory->where(array('id' => $id))->getField('specification_ids');
        $m_specification = new \app\xwam\model\ProductSpecificationModel();
        $list = $m_specification->where(array('pid' => 0))->order('sort, id desc')->select();
        $this->assign('list', $list);
        $this->assign('specification_ids', $specification_ids);
        $this->assign('cid', $id);
        return view();
    }

    public function specification_picker_save() {
        $ids = I('id');
        $cid = I('cid');
        if (!$ids || !$cid) {
            $this->error('参数有误!');
        }
        $mCategory = new \app\xwam\model\ProductCategoryModel();
        $result = $mCategory->where(array('id' => $cid))->setField('specification_ids', implode(',', $ids));
        if (false === $result) {
            $this->error('出错啦：' . $mCategory->getDbError());
        } else {
            $this->dialogJump(true);
        }
    }
    // 2017-3-18 同步用户表的部门标题
    function synchronous(){
        \app\xwam\model\ProductCategoryModel::updateForeignTitle(true);
    }

}
